/*
workbook:excel工作簿
sheet:表页
column:多列-width
row:多行-height
cell:单元格
workbook = {
	defaultrows: 20,
	defaultcols: 20,
	defaultcolwidth : 100,
	defaultrowheight : 25,
	worksheet : [{
		defaultrows: 10,
		defaultcols: 10,
		defaultcolwidth : 120,
		defaultrowheight : 30,
		column : {
			colkey : {
				width : 120
			}
		},
		row : {
			rowkey1 : {
				height : 35,
				cell : {
					cellkey1 : {
						mergedown : 5
					}
				}
			},
			rowkey2 : {
				height : 35,
				cell : {
					cellkey1 : {
						data : '12',
						datatype : 'number'
					}
				}
			}
		}
	}]
}
*/


var data = '{"worksheet":[{"defaultrows":10,"defaultcols":9,"defaultcolwidth":100,"defaultrowheight":25,"column":{"col_3":{"width":120},"col_8":{"width":298}},"row":{"row_3":{"height":35},"cell":{"cell_1_1":{"mergeAcross":3,"width":420,"data":"工时预算表","text-align":"center","text-valign":"middle","font-size":"20px","font-weight":"bold"},"cell_2_1":{"mergeDown":4,"height":135,"data":"系统","text-align":"center","text-valign":"middle","font-size":"20px"},"cell_2_2":{"data":"功能一","text-valign":"middle","text-align":"center","font-size":"14px"},"cell_3_2":{"data":"功能二","text-align":"center","text-valign":"middle","font-size":"14px"},"cell_4_2":{"data":"功能三","text-valign":"middle","text-align":"center","font-size":"14px"},"cell_5_2":{"data":"功能四","text-valign":"middle","text-align":"center","font-size":"14px"},"cell_6_2":{"data":"功能五","text-valign":"middle","text-align":"center","font-size":"14px"},"cell_2_3":{"data":"1","text-align":"center","text-valign":"middle","font-size":"14px"},"cell_3_3":{"data":"2","text-valign":"middle","text-align":"center","font-size":"14px"},"cell_4_3":{"data":"4","text-align":"center","text-valign":"middle","font-size":"14px"},"cell_5_3":{"data":"5","text-valign":"middle","text-align":"center","font-size":"14px"},"cell_6_3":{"data":"8","text-valign":"middle","text-align":"center","font-size":"14px"},"cell_2_4":{"mergeDown":1,"height":60,"data":"翟晋江","text-valign":"middle","text-align":"center"},"cell_4_4":{"mergeDown":2,"height":75,"data":"世纪空间","text-valign":"middle","text-align":"center"},"cell_1_5":{"data":"双击排序"},"cell_4_5":{"data":"2018-08-18","datatype":"date"},"cell_5_5":{"data":"黑体字","font-family":"黑体"},"cell_1_6":{"data":"","text-align":"center"},"cell_2_6":{"data":"水果名称","text-align":"center"},"cell_3_6":{"data":"香蕉","text-align":"center"},"cell_4_6":{"data":"苹果","text-align":"center"},"cell_5_6":{"data":"梨","text-align":"center"},"cell_6_6":{"data":"猕猴桃","text-align":"center"},"cell_7_6":{"data":"西瓜","text-align":"center"},"cell_8_6":{"data":"葡萄","text-align":"center"},"cell_9_6":{"data":"香瓜","text-valign":"middle","text-align":"center"},"cell_10_6":{"data":"荔枝","text-valign":"middle","text-align":"center"},"cell_1_7":{"data":"3"},"cell_2_7":{"data":"水果数量"},"cell_3_7":{"data":"5"},"cell_4_7":{"data":"8"},"cell_5_7":{"data":"32"},"cell_6_7":{"data":"76"},"cell_7_7":{"data":"44"},"cell_8_7":{"data":"1"},"cell_9_7":{"data":"900","datatype":"number","text-valign":"middle","text-align":"center"},"cell_10_7":{"data":"20","text-valign":"middle","text-align":"center"},"cell_1_8":{"data":"北京市地图展示"},"cell_2_8":{"mergeDown":8,"height":235,"data":"","datatype":"image"},"cell_1_9":{"data":"零"},"cell_2_9":{"data":"一"},"cell_3_9":{"data":"二"},"cell_4_9":{"data":"三"},"cell_5_9":{"data":"四"},"cell_6_9":{"data":"五"},"cell_7_9":{"data":"六"},"cell_8_9":{"data":"七"},"cell_9_9":{"data":"八"},"cell_10_9":{"data":"九"},"cell_8_1":{"mergeAcross":1,"width":200,"data":"坚决执行操作："},"cell_10_1":{"mergeAcross":2,"width":320,"data":"你准备干什么啊...","text-align":"right"}},"row_1":{"height":45}}}]}';	

var data1 = '{"worksheet":[{"defaultrows":10,"defaultcols":10,"defaultcolwidth":100,"defaultrowheight":25,"column":{"col_1":{"width":52},"col_9":{"width":280}},"row":{"cell":{"cell_1_1":{"data":"序号","font-size":"16px","font-weight":"bold"},"cell_2_1":{"data":"1"},"cell_3_1":{"data":"2"},"cell_4_1":{"data":"3"},"cell_5_1":{"data":"4"},"cell_6_1":{"data":"5"},"cell_7_1":{"data":"6"},"cell_8_1":{"data":"7"},"cell_9_1":{"data":"8"},"cell_10_1":{"data":"9"},"cell_1_2":{"data":"姓名","font-size":"16px","font-weight":"bold"},"cell_2_2":{"data":"翟晋江"},"cell_3_2":{"data":"王卫星"},"cell_4_2":{"data":"王勇强"},"cell_5_2":{"data":"刘泽文"},"cell_6_2":{"data":"张贺"},"cell_7_2":{"data":"廖光波"},"cell_8_2":{"data":"吴化龙"},"cell_9_2":{"data":"聂靖芮"},"cell_10_2":{"data":"刘奴峰"},"cell_1_3":{"data":"级别","font-size":"16px","font-weight":"bold"},"cell_2_3":{"data":"管理"},"cell_3_3":{"data":"高级"},"cell_4_3":{"data":"助高"},"cell_5_3":{"data":"助高"},"cell_6_3":{"data":"助高"},"cell_7_3":{"data":"助高"},"cell_8_3":{"data":"中级"},"cell_9_3":{"data":"见习"},"cell_10_3":{"data":"见习"},"cell_1_4":{"data":"电话号码","font-size":"16px","font-weight":"bold"},"cell_1_5":{"data":"出生日期","font-size":"16px","font-weight":"bold"},"cell_1_6":{"data":"年龄","font-size":"16px","font-weight":"bold"},"cell_1_7":{"data":"最后登录时间","font-size":"16px","font-weight":"bold"},"cell_1_8":{"data":"指导老师","font-size":"16px","font-weight":"bold"},"cell_1_9":{"data":"籍贯","font-size":"16px","font-weight":"bold"}},"row_1":{"height":38}}}]}';

var data2 = '{"worksheet":[{"defaultrows":12,"defaultcols":4,"defaultcolwidth":100,"defaultrowheight":25,"column":{"col_2":{"width":99},"col_4":{"width":206}},"row":{"cell":{"cell_1_1":{"mergeAcross":3,"width":505,"data":"保护区野外调查表","text-align":"center","text-valign":"middle","font-size":"20px","font-weight":"bold"},"cell_2_1":{"data":"调查人"},"cell_2_3":{"data":"调查日期","text-align":"center"},"cell_5_1":{"data":"鸟类名称","font-weight":"bold","font-size":"16px","text-align":"center"},"cell_5_2":{"data":"鸟类数量","font-weight":"bold","font-size":"16px","text-align":"center"},"cell_5_4":{"data":"备注","font-weight":"bold","font-size":"16px","text-align":"center"},"cell_6_1":{"data":"东方白鹳","text-align":"center"},"cell_7_1":{"data":"尖嘴鹤","text-align":"center"},"cell_8_1":{"data":"蓝翅八色鸫","text-align":"center"},"cell_9_1":{"data":"雪鸮","text-align":"center"},"cell_10_1":{"data":"朱鹮","text-align":"center"},"cell_11_1":{"data":"花头鹦鹉","text-align":"center"},"cell_12_1":{"data":"疣鼻天鹅","text-align":"center"},"cell_3_1":{"data":"调查随行人员","text-align":"center"},"cell_3_2":{"mergeAcross":2,"width":405,"data":"王卫星、刘泽文、王勇强、廖光波、张贺","text-align":"left"},"cell_4_1":{"data":"天气情况","text-align":"center"},"cell_4_3":{"data":"温度","text-align":"center"},"cell_2_2":{"data":"张贺","text-align":"left"},"cell_5_3":{"data":"生境","text-align":"center","text-valign":"middle","font-size":"16px","font-weight":"bold"}},"row_1":{"height":41},"row_5":{"height":32}}}]}';

//拖拽选中的cells
var selectedcells = {};
//选中行
var selectedrows = {};
//选中列
var selectedcols = {};

//行列header的大小
var offset_left = 25;
var offset_top = 25;




//当前编辑的行列号
var currentCol = 1;
var currentRow = 1;
//是否拖拽状态
var bDrap = false;
//选中列
var selectedCol = null;
var selectedRow = null;
//正在调整行列大小
var cellresize = false;
var clickselected = false;
var workbook = {};
var currentSheet = {};
//被合并的单元格
var mergecells = {};
var canvas = null;
var cxt = null;

//cell的绘制对象
var canvascells = {};

var canvaswidth = 0; 
var canvasheight = 0;

//
$(document).ready(function(){
	/**
	 * 获取 canvas 画布
	 * 获取 canvas 绘图上下文环境
	 */
	canvas = $('#canvas')[0];
	cxt = canvas.getContext('2d');

	bindEvent();

	loadWorkbook();
});


function initWorkBook(){
	workbook = {};
	workbook.worksheet = [];
	var sheet = {};
	sheet.defaultrows = 10;
	sheet.defaultcols = 10;
	sheet.defaultcolwidth = 100;
	sheet.defaultrowheight = 25;
	sheet.column = {};
	sheet.row = {};
	sheet.row.cell = {};
	/*
	sheet.column = {};
	sheet.column.col_3 = {};
	sheet.column.col_3.width = 120;

	sheet.row = {};
	sheet.row.row_3 = {};
	sheet.row.row_3.height = 35;
	*/

	workbook.worksheet.push(sheet);
	return workbook;
}

function reloadWorkBook(){
	cxt.clearRect(0,0,canvas.width,canvas.height);
	//调整工作簿大小
	resizeWorkbookSize(currentSheet);
	createWorkbookHeaderRow(currentSheet);
	createWorkbookHeaderCol(currentSheet);
	createWorkbookSheetCell(currentSheet);

	highlightedSelectedCells();
	highlightedSelectedRowsORCols();
	resetWorkBookButton();
}

function resetWorkBookButton(){
	var selCellsLength = Object.keys(selectedcells).length;
	if(selCellsLength  > 1){
		$("#mergecellbtn").removeAttr('disabled');
		$("#cancelmergecellbtn").removeAttr('disabled');
		$("button[name='createchart']").removeAttr('disabled');
		$("button[name='cellccp']").removeAttr('disabled');
	}else{
		$("#mergecellbtn").attr('disabled','disabled');
		$("#cancelmergecellbtn").attr('disabled','disabled');
		$("button[name='createchart']").attr('disabled','disabled');
		$("button[name='cellccp']").attr('disabled','disabled');
	}

	if(selCellsLength  == 1){
		$("button[name='cellccp']").removeAttr('disabled');
	}else{
		//$("button[name='cellccp']").attr('disabled','disabled');
	}

	if(selCellsLength  < 1){
		$("button[name='setcellalign']").attr('disabled','disabled');
		$("button[name='setcellfontstyle']").attr('disabled','disabled');
		$("select[name='setcellfontstyle']").attr('disabled','disabled');
	}else{
		$("button[name='setcellalign']").removeAttr('disabled');
		$("button[name='setcellfontstyle']").removeAttr('disabled');
		$("select[name='setcellfontstyle']").removeAttr('disabled');
	}


	//是否选中列
	var selColsLength = Object.keys(selectedcols).length;
	if(selColsLength > 0){
		$("#removecolbtn").removeAttr('disabled');
		$("button[name='colopt']").removeAttr('disabled');
		$("#colascsortbtn1").removeAttr('disabled');
		$("#colascsortbtn2").removeAttr('disabled');
		$("#coldescsortbtn1").removeAttr('disabled');
		$("#coldescsortbtn2").removeAttr('disabled');
	}else{
		$("#removecolbtn").attr('disabled','disabled');
		$("button[name='colopt']").attr('disabled','disabled');
		$("#colascsortbtn1").attr('disabled','disabled');
		$("#colascsortbtn2").attr('disabled','disabled');
		$("#coldescsortbtn1").attr('disabled','disabled');
		$("#coldescsortbtn2").attr('disabled','disabled');
	}

	var selRowsLength = Object.keys(selectedrows).length;
	if(selRowsLength > 0){
		$("#removerowbtn").removeAttr('disabled');
		$("button[name='rowopt']").removeAttr('disabled');
	}else{
		$("#removerowbtn").attr("disabled",'disabled');
		$("button[name='rowopt']").attr('disabled','disabled');
	}
}


function createWorkBook(workbook){
	if(!workbook){
		//初始化工作簿
		workbook = initWorkBook();
		mergecells = {};
	}
	
	//绘制工作簿
	for(var i in workbook.worksheet){
		var sheet = workbook.worksheet[i];
		currentSheet = sheet;
		//调整工作簿大小
		resizeWorkbookSize(sheet);
		createWorkbookHeaderRow(sheet);
		createWorkbookHeaderCol(sheet);
		createWorkbookSheetCell(sheet);
		resetWorkBookButton();
	}
}

function createWorkbookHeaderRow(sheet){
	var x = offset_left;
	var y = 0;
	for (var col = 1; col <= sheet.defaultcols; col++) {
		var w = getColWidth(sheet,'col_'+col);
		var h = offset_top;
		
		cxt.fillStyle = "#DFE3EB";
		cxt.fillRect(x,y,w,h);
		cxt.lineWidth = 0.5;
		cxt.strokeStyle = "#a0a0a0";
		cxt.strokeRect(x,y,w,h);
		cxt.textAlign = 'center';
		cxt.textBaseline = 'middle';
		cxt.fillStyle = "#000000";
		cxt.fillText('列_'+col,x+w/2,y+h/2);
		x += w;	
	}
}

function createWorkbookHeaderCol(sheet){
	var x = 0;
	var y = offset_top;
	for(var row = 1; row <= sheet.defaultrows; row++){
		var w = offset_left;
		var h = getRowHeight(sheet,'row_'+row);
		
		cxt.fillStyle = "#DFE3EB";
		cxt.fillRect(x,y,w,h);
		cxt.lineWidth = 0.5;
		cxt.strokeStyle = "#a0a0a0";
		cxt.strokeRect(x,y,w,h);
		cxt.textAlign = 'center';
		cxt.textBaseline = 'middle';
		cxt.fillStyle = "#000000";
		cxt.fillText(row,x+w/2,y+h/2);
		y += h;	
	}
}

function createWorkbookSheetCell(sheet){
	
	var x = offset_left;
	var y = offset_top;
	// 循环行
	for(var row = 1; row <= sheet.defaultrows; row++){
		var h = getRowHeight(sheet,'row_' + row);
		for (var col = 1; col <= sheet.defaultcols; col++) {
			var w = getColWidth(sheet,'col_' + col);
			var cellkey = 'cell_' + row + '_' + col;
			var v = getCellData(sheet,cellkey);
			//console.log(cellkey);
			//处理合并单元格
			var mergeDown = getCellAttr(sheet,cellkey,'mergeDown');
			var mergeAcross = getCellAttr(sheet,cellkey,'mergeAcross');
			var mergeWidth = 0;
			var mergeHeight = 0;
			if(mergeDown != null){
				//console.log('多行合并');
				mergeHeight = h;
				for(var i = 1; i <= mergeDown;i ++){
					var mcellkey = 'cell_' + (row + i) + '_' + col;
					mergecells[mcellkey] = true;
					//console.log((row + i) + '行,' + col + '列被合并');
					var mergeh = getRowHeight(sheet,'row_' + (row + i));
					mergeHeight += mergeh;
				}
				
				//计算高度
				setCellAttr(sheet,cellkey,'height',mergeHeight);
			}
			if(mergeAcross != null){
				mergeWidth = w;
				//console.log(mergeAcross + '列被合并');
				for(var i = 1; i <= mergeAcross;i ++){
					var mcellkey = 'cell_' + row + '_' + (col + i);
					mergecells[mcellkey] = true;
					//console.log(row + '行,' + (col + i) + '列被合并');
					var mergew = getColWidth(sheet,'col_' + (col + i));
					//console.log(mergew);
					mergeWidth += mergew;
				}
				//console.log(mergeWidth);
				//计算宽度
				setCellAttr(sheet,cellkey,'width',mergeWidth);
			}
			//没有被合并，进行绘制，如果合并则不进行绘制
			if(!mergecells[cellkey]){
				var fontstyle = getCellFontStyle(sheet,cellkey);
				cxt.font = fontstyle;
				cxt.fillStyle = 'blue';
				var textAlign = getCellAttr(sheet,cellkey,'text-align');
				var textVAlign = getCellAttr(sheet,cellkey,'text-valign');
				var datatype = getCellAttr(sheet,cellkey,'datatype');
				var textX = x;
				var textY = y;
				if(textAlign != null){
					if(textAlign == 'left'){
						textX = x;
					}
					if(textAlign == 'center'){
						textX = x + w/2;
					}
					if(textAlign == 'right'){
						textX = x + w;
					}
				}else{
					textX = x + w/2;
				}
				if(textVAlign == null){
					textVAlign = 'middle';
				}
				cxt.textAlign = textAlign;
				cxt.textBaseline = textVAlign;
				//cxt.textDecoration = 'underline';
				cxt.lineWidth = 0.2;
				cxt.strokeStyle = "#000000";
				if(mergeWidth != 0){
					if(textAlign != null){
						if(textAlign == 'left'){
							textX = x;
						}
						if(textAlign == 'center'){
							textX = x + mergeWidth/2;
						}
						if(textAlign == 'right'){
							textX = x + mergeWidth;
						}
					}
					if(datatype == 'image' && v != ''){
						//创建新的图片对象
						var img = new Image();
						//指定图片的URL
						img.src = v;
						//浏览器加载图片完毕后再绘制图片
						//img.onload = function(){
							//以Canvas画布上的坐标(10,10)为起始点，绘制图像
							cxt.drawImage(img, x,y);
						//}
					}else{
						cxt.fillText(v,textX,y+h/2);
					}
					
					cxt.strokeRect(x,y,mergeWidth,h);
					var canvascell = {};
					canvascell.row = row;
					canvascell.col = col;
					canvascell.x = x;
					canvascell.y = y;
					canvascell.width = mergeWidth;
					canvascell.height = h;
					canvascells[cellkey] = canvascell;
				}else if(mergeHeight != 0){
					if(textVAlign != null){
						if(textVAlign == 'top'){
							textY = y;
						}
						if(textVAlign == 'middle'){
							textY = y + mergeHeight/2;
						}
						if(textVAlign == 'bottom'){
							textY = y + mergeHeight;
						}
					}
					if(datatype == 'image' && v != ''){
						//创建新的图片对象
						var img = new Image();
						img.setAttribute('crossOrigin', 'anonymous');
						//指定图片的URL
						img.src = v;
						//浏览器加载图片完毕后再绘制图片
						//img.onload = function(){
							//以Canvas画布上的坐标(10,10)为起始点，绘制图像
							cxt.drawImage(img, x, y,w,mergeHeight);
						//}       
					}else{
						cxt.fillText(v,textX,textY);
					}
					cxt.strokeRect(x,y,w,mergeHeight);
					var canvascell = {};
					canvascell.row = row;
					canvascell.col = col;
					canvascell.x = x;
					canvascell.y = y;
					canvascell.width = w;
					canvascell.height = mergeHeight;
					canvascells[cellkey] = canvascell;
				}else{
					if(datatype == 'image' && v != ''){
						//创建新的图片对象
						var img = new Image();
						//指定图片的URL
						img.src = v;
						//浏览器加载图片完毕后再绘制图片
						//img.onload = function(){
							//以Canvas画布上的坐标(10,10)为起始点，绘制图像
							cxt.drawImage(img, x,y); 
						//}
					}else{
						cxt.fillText(v,textX,y+h/2);
					}
					cxt.strokeRect(x,y,w,h);
					var canvascell = {};
					canvascell.row = row;
					canvascell.col = col;
					canvascell.x = x;
					canvascell.y = y;
					canvascell.width = w;
					canvascell.height = h;
					canvascells[cellkey] = canvascell;
				}
				
				
			}
			
			x += w;	
		}
		x = offset_left;
		y += h;	
	}
}

function getCanvasCellByCellkey(cellkey){
	return canvascells[cellkey];
}

/**
* 根据鼠标位置获取当前行列
*/
function getWorkbookSheetCellByXY(sheet,mousex,mousey){
	var selcell = {};
	var x = offset_left;
	var y = offset_top;
	var bfind = false;
	// 循环行
	for(var row = 1; row <= sheet.defaultrows; row++){
		var h = getRowHeight(sheet,'row_' + row);
		for (var col = 1; col <= sheet.defaultcols; col++) {
			var w = getColWidth(sheet,'col_' + col);
			var cellkey = 'cell_' + row + '_' + col;
			var cell = {};
			cell.row = row;
			cell.col = col;
			cell.x = x;
			cell.y = y;
			cell.width = w;
			cell.height = h;
			var minx = x;
			var maxx = minx + w;
			var miny = y;
			var maxy = miny + h;
			//获取单元格的宽度和高度，如果
			if(!mergecells[cellkey]){
				var cellW = getCellAttr(sheet,cellkey,'width');
				var cellH = getCellAttr(sheet,cellkey,'height');
				//console.log(cellW);
				//console.log(cellH);
				if(cellW != null){
					maxx = minx + cellW;
					cell.width = cellW;
				}else{
					cell.width = w;
				}
				if(cellH != null){
					maxy = miny + cellH;
					cell.height = cellH;
				}else{
					cell.height = h;
				}
			}else{
				x += w;
				continue;
			}
			if(mousex >= minx && mousex < maxx && mousey >= miny && mousey < maxy){
				selcell = cell;
				bfind = true;
			}
			if(bfind){
				break;
			}
			x += w;
		}
		if(bfind){
			break;
		}
		x = offset_left;
		y += h;	

	}
	return selcell;
}


function isResizeCellWidth(currentSheet,mousex,mousey){
	var sheet = currentSheet;
	var bcolresize = false;
	var x = offset_left;
	var y = 0;
	for (var col = 1; col <= sheet.defaultcols; col++) {
		var w = getColWidth(sheet,'col_'+col);
		var h = offset_top;
		var _minx = x + 6;
		var _maxx = x + w - 6;

		x += w;	
		var minx = x - 6;
		var maxx = x + 6;
		var miny = 0;
		var maxy = h;

		//console.log(mousex + ':' +mousey);
		//console.log(_minx + ':' +_maxx + ':' +miny + ':' +maxy);
		if(mousex >= minx && mousex < maxx && mousey >= miny && mousey < maxy){
			//console.log("选中列边框:"+col+"列");
			$("#canvas").css("cursor","col-resize");
			selectedCol = col;
			//cellresize = true;
			bcolresize = true;
			break;
		}else if(mousex >= _minx && mousex < _maxx && mousey >= miny && mousey < maxy){
			$("#canvas").css("cursor","move");
			clickselected = true;
			bcolresize = true;
			break;
		}else{
			$("#canvas").css("cursor","default");
			bcolresize = false;
			selectedCol = null;
		}
	}
	return bcolresize;
}

function getCurrentColByXY(currentSheet,mousex,mousey){
	var sheet = currentSheet;
	var currentcol = null;
	var x = offset_left;
	var y = 0;
	for (var col = 1; col <= sheet.defaultcols; col++) {
		var w = getColWidth(sheet,'col_'+col);
		var h = offset_top;
		var _minx = x + 6;
		var _maxx = x + w - 6;
		x += w;	
		var miny = 0;
		var maxy = h;
		//console.log(mousex + ':' +mousey);
		//console.log(_minx + ':' +_maxx + ':' +miny + ':' +maxy);
		if(mousex >= _minx && mousex < _maxx && mousey >= miny && mousey < maxy){
			currentcol = col;
			break;
		}
	}
	return currentcol;
}


function isResizeCellHeight(currentSheet,mousex,mousey){
	var sheet = currentSheet;
	var x = 0;
	var y = offset_top;
	for(var row = 1; row <= sheet.defaultrows; row++){
		var w = offset_left;
		var h = getRowHeight(sheet,'row_'+row);
		var _miny = y + 6;
		var _maxy = y + h - 6;
		y += h;	
		var minx = 0;
		var maxx = offset_left;
		var miny = y - 6;
		var maxy = y + 6;

		if(mousex >= minx && mousex < maxx && mousey >= miny && mousey < maxy){
			$("#canvas").css("cursor","row-resize");
			//cellresize = true;
			selectedRow = row;
			break;
		}else if(mousex >= minx && mousex < maxx && mousey >= _miny && mousey < _maxy){
			$("#canvas").css("cursor","move");
			clickselected = true;
			break;
		}else{
			$("#canvas").css("cursor","default");
			//cellresize = false;
			selectedRow = null;
		}
	}
}

function getCurrentRowByXY(currentSheet,mousex,mousey){
	var currentrow = null;
	var sheet = currentSheet;
	var x = 0;
	var y = offset_top;
	for(var row = 1; row <= sheet.defaultrows; row++){
		var w = offset_left;
		var h = getRowHeight(sheet,'row_'+row);
		var _miny = y + 6;
		var _maxy = y + h - 6;
		y += h;	
		var minx = 0;
		var maxx = offset_left;

		if(mousex >= minx && mousex < maxx && mousey >= _miny && mousey < _maxy){
			currentrow = row;
			break;
		}
	}
	return currentrow;
}


function getRowHeight(sheet,rowkey){
	//如果行单独设置，先使用行，否则默认即可
	if(sheet.row && sheet.row[rowkey] && sheet.row[rowkey].height){
		return sheet.row[rowkey].height;
	}
	return sheet.defaultrowheight;
}

function getColWidth(sheet,colkey){
	//如果列单独设置，先使用列，否则默认即可
	if(sheet.column && sheet.column[colkey] && sheet.column[colkey].width){
		return sheet.column[colkey].width;
	}
	return sheet.defaultcolwidth;
}

function getColObject(sheet,colkey){
	//如果列单独设置，先使用列，否则默认即可
	var colObject = '';
	if(sheet.column && sheet.column[colkey]){
		colObject = sheet.column[colkey];
	}
	return colObject;
}

function getRowObject(sheet,rowkey){
	//如果列单独设置，先使用列，否则默认即可
	var rowObject = '';
	if(sheet.row && sheet.row[rowkey]){
		rowObject = sheet.row[rowkey];
	}
	return rowObject;
}

function getRowCellsObject(sheet,row){
	var rowCells = {};
	for (var col = 1; col <= sheet.defaultcols; col++) {
		//如果列单独设置，先使用列，否则默认即可
		var cellkey = 'cell_' + row + '_' + col;
		var cellObject = '';
		if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]){
			cellObject = sheet.row.cell[cellkey];
		}
		rowCells[cellkey] = cellObject;
	}
	return rowCells;
}

function getColCellsObject(sheet,col){
	var colCells = {};
	for (var row = 1; row <= sheet.defaultrows; row++) {
		//如果列单独设置，先使用列，否则默认即可
		var cellkey = 'cell_' + row + '_' + col;
		var cellObject = '';
		if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]){
			cellObject = sheet.row.cell[cellkey];
		}
		colCells[cellkey] = cellObject;
	}
	return colCells;
}

function getCellObject(sheet,cellkey){
	//如果列单独设置，先使用列，否则默认即可
	var cellObject = '';
	if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]){
		cellObject = sheet.row.cell[cellkey];
	}
	return cellObject;
}

function getCellData(sheet,cellkey){
	//如果列单独设置，先使用列，否则默认即可
	var cellvalue = '';
	if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]&& sheet.row.cell[cellkey].data){
		cellvalue = sheet.row.cell[cellkey].data;
	}
	return cellvalue;
}

function getCellDatatype(sheet,cellkey){
	//如果列单独设置，先使用列，否则默认即可
	var celldatatype = 'text';
	if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]&& sheet.row.cell[cellkey].datatype){
		celldatatype = sheet.row.cell[cellkey].datatype;
	}
	return celldatatype;
}

function setCellData(sheet,cellkey,cellvalue){
	//如果列单独设置，先使用列，否则默认即可
	if(!sheet.row){
		sheet.row = {};
	}
	if(!sheet.row.cell){
		sheet.row.cell = {};
	}
	if(!sheet.row.cell[cellkey]){
		sheet.row.cell[cellkey] = {};
	}
	sheet.row.cell[cellkey].data = cellvalue;	
	currentSheet = sheet;
}

function setCellDataType(sheet,cellkey,datatype){
	//如果列单独设置，先使用列，否则默认即可
	if(!sheet.row){
		sheet.row = {};
	}
	if(!sheet.row.cell){
		sheet.row.cell = {};
	}
	if(!sheet.row.cell[cellkey]){
		sheet.row.cell[cellkey] = {};
	}
	sheet.row.cell[cellkey].datatype = datatype;	
	currentSheet = sheet;
}

/*
 * font参数的值分为
 * font-style: normal(正常), italic(斜体字), oblique(倾斜字体) 后两种在网页端一般没什么区别
 * font-variant: normal(正常), small-caps(英文小写字母变成小的大写)
 * font-weight: normal(正常), bold(加粗) 100-900(一般不用)
 * font-size: 文字大小
 * font-family: 字体样式
 */
//cxt.font = "";
function getCellFontStyle(sheet,cellkey){
	//如果列单独设置，先使用列，否则默认即可
	var fontfamily = '微软雅黑';
	var fontsize = '12px';
	var fontweight = 'normal';
	var fontstyle = 'normal';
	if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]&& sheet.row.cell[cellkey]['font-family']){
		fontfamily = sheet.row.cell[cellkey]['font-family'];
	}
	if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]&& sheet.row.cell[cellkey]['font-size']){
		fontsize = sheet.row.cell[cellkey]['font-size'];
	}
	if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]&& sheet.row.cell[cellkey]['font-weight']){
		fontweight = sheet.row.cell[cellkey]['font-weight'];
		//fontweight = fontweight == 'normal'?'bold':'normal';
	}
	if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]&& sheet.row.cell[cellkey]['font-style']){
		fontstyle = sheet.row.cell[cellkey]['font-style'];
		//fontstyle = fontstyle == 'normal'?'italic':'normal';
	}
	return fontstyle + ' normal '+fontweight+' ' + fontsize + ' ' + fontfamily;
}

function changeColWidth(sheet,colkey,resizeW){
	if(!sheet.column){
		sheet.column = {};
	}
	if(!sheet.column[colkey]){
		sheet.column[colkey] = {};
	}
	sheet.column[colkey].width = resizeW;
	currentSheet = sheet;
}


function changeRowHeight(sheet,rowkey,resizeH){
	if(!sheet.row){
		sheet.row = {};
	}
	if(!sheet.row[rowkey]){
		sheet.row[rowkey] = {};
	}
	sheet.row[rowkey].height = resizeH;
	
	currentSheet = sheet;
}

function setCellAttr(sheet,cellkey,attrname,attrvalue){
	if(!sheet.row){
		sheet.row = {};
	}
	if(!sheet.row.cell){
		sheet.row.cell = {};
	}

	if(!sheet.row.cell[cellkey]){
		sheet.row.cell[cellkey] = {};
	}
	sheet.row.cell[cellkey][attrname] = attrvalue;
	
	currentSheet = sheet;
}

function getCellAttr(sheet,cellkey,attrname){
	var cellvaluel = null;
	if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]&& sheet.row.cell[cellkey][attrname]){
		cellvaluel = sheet.row.cell[cellkey][attrname];
	}
	return cellvaluel;
}

function removeCellAttr(sheet,cellkey,attrname){
	if(sheet.row && sheet.row.cell && sheet.row.cell[cellkey]&& sheet.row.cell[cellkey][attrname]){
		delete sheet.row.cell[cellkey][attrname];
	}
}



function resizeWorkbookSize(sheet){
	//此处有问题，根据行列进行计算得出，不能固定长度==zjj
	//var canvaswidth = sheet.defaultcols * sheet.defaultcolwidth;
	//var canvasheight = sheet.defaultrows * sheet.defaultrowheight;
	canvaswidth = 0;
	canvasheight = 0;
	for(var row = 1; row <= sheet.defaultrows; row++){
		var h = getRowHeight(sheet,'row_' + row);			
		canvasheight += h;
	}
	for (var col = 1; col <= sheet.defaultcols; col++) {
		var w = getColWidth(sheet,'col_' + col);
		canvaswidth += w;
	}
	//console.log(canvaswidth + '::' + canvasheight);
	$("#canvas").attr("width",canvaswidth + offset_left);
	$("#canvas").attr("height",canvasheight + offset_top);
	drawCopyright(canvaswidth,canvasheight);
}

function drawCopyright(x,y){
	cxt.strokeStyle = "#a0a0a0";
	cxt.fillStyle = "#000000";
	cxt.textAlign = 'right';
	cxt.textBaseline = 'bottom';
	cxt.fillText('@copyright:世纪空间',x,y);
}

function drawMouseMoveInfo(x,y,mousex,mousey,row,col){
	//cxt.strokeStyle = "#a0a0a0";
	//cxt.fillStyle = "#000000";
	//cxt.textAlign = 'left';
	//cxt.textBaseline = 'bottom';
	//cxt.fillText('x:'+mousex+',y:'+mousey+',row:'+row+',col:'+col,offset_left,y);
	if(row && col){
		$("#mouseinfo").show();
		$("#mouseinfo").text('x:'+mousex+',y:'+mousey+',row:'+row+',col:'+col);
	}else{
		$("#mouseinfo").hide();
	}
}

function highlightedSelectedCells(){
	// 循环行
	for (var i in selectedcells) {
		var cell = selectedcells[i];
		var x = cell.x;
		var y = cell.y;
		var w = cell.width;
		var h = cell.height;
		cxt.lineWidth = 0.8;
		cxt.strokeStyle = "#FF0000";
		cxt.strokeRect(x,y,w,h);
	}
}

function highlightedSelectedRowsORCols(){

	// 高亮选中行
	for (var i in selectedrows) {
		var selrow = selectedrows[i];
		var selrowh = getRowHeight(currentSheet,'row_'+selrow);
		var totalw = 0;
		var totalh = 0;
		for (var col = 1; col <= currentSheet.defaultcols; col++) {
			var w = getColWidth(currentSheet,'col_'+col);
			totalw += w;
		}
		for (var row = 1; row < selrow; row++) {
			var h = getRowHeight(currentSheet,'row_'+row);
			totalh += h;	
		}
		var x = offset_left;
		var y = offset_top + totalh;
		cxt.lineWidth = 0.8;
		cxt.strokeStyle = "#FF0000";
		cxt.strokeRect(x,y,totalw,selrowh);
	}

	// 高亮选中列
	for (var i in selectedcols) {
		var selcol = selectedcols[i];
		var selcolw = getColWidth(currentSheet,'col_'+selcol);
		var totalw = 0;
		var totalh = 0;
		for (var col = 1; col < selcol; col++) {
			var w = getColWidth(currentSheet,'col_'+col);
			totalw += w;
		}
		for (var row = 1; row <= currentSheet.defaultrows; row++) {
			var h = getRowHeight(currentSheet,'row_'+row);
			totalh += h;	
		}
		var x = offset_left + totalw;
		var y = offset_top;
		cxt.lineWidth = 0.8;
		cxt.strokeStyle = "#FF0000";
		cxt.strokeRect(x,y,selcolw,totalh);
	}
	//selectedrows = {};
	//selectedcols = {};
}
